

const { ccclass, property } = cc._decorator;

/**
 * 小兵实体组件
 */
@ccclass
export default class UnitView extends cc.Component {
 
    @property({type:cc.ProgressBar,tooltip:'血条'})
    hpProgressBar: cc.ProgressBar = null;

    
    @property({type:cc.Sprite,tooltip:'血条图片'})
    bloodImg: cc.Sprite = null;

    @property({type:[cc.SpriteFrame],tooltip:'血条图片'})
    bloodFrames: cc.SpriteFrame[] = [];

    /**
     * 血条头顶距离
     */
    barPos = new cc.Vec3(0, 90, 0);
    /**
     * 数据
     */
    data = null;
    /**
     * 是否显示血条
     */
    isShow = false;
    onLoad() {

    }
    /**
     * 初始化小兵
     * @param data 数据
     * @param isPlayer 是否为玩家
     */
    init(data, isPlayer = false) {
        this.data = data;
        this.hpProgressBar.progress = this.data.currentHp / this.data.maxhp;
        this.hpProgressBar.node.parent = cc.find('Canvas/mapUI');
        this.hpProgressBar.node.active = false;
        this.bloodImg.spriteFrame = this.bloodFrames[Number(isPlayer)];
    }
    /**
     * 显示血条
     * @param id 
     */
    change(id: string | number) {
        if (id == 'currentHp') {
            this.hpProgressBar.progress = this.data.currentHp / this.data.maxhp;
            if (!this.isShow) {
                this.isShow = true;
                this.hpProgressBar.node.active = true;
            }
            if (this.data.currentHp <= 0) {
                this.isShow = false;
                this.hpProgressBar.node.active = false;
            }
        }
    }
    start() {

    }
    lateUpdate(dt) {
        if (this.isShow) {
            this.hpProgressBar.node.position = this.node.position.add(this.barPos);
        }
    }
}
